﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class FrontEnd_Register : System.Web.UI.Page
{
    public String message = "";
    protected BLO.User user;
    protected void Page_Load(object sender, EventArgs e)
    {
        getDataFromControl();
    }
    public void getDataFromControl()
    {
        String kindSubmit = Request.Form.Get("hddKindSubmit");
        if (kindSubmit == "register")
        {
            String s = Request.Form.Get("txtPass");
            String question1 = "";
            String question2 = "";
            if (Request.Form.Get("txtQuestion1") == "") question1 = Request.Form.Get("cboQuestion1");
            else question1 = Request.Form.Get("txtQuestion1");
            if (Request.Form.Get("txtQuestion2") == "") question2 = Request.Form.Get("cboQuestion2");
            else question2 = Request.Form.Get("txtQuestion2");
            user = new BLO.User();
            user.Info.ID = Request.Form.Get("txtUserID");
            user.Info.PassWord = Request.Form.Get("txtPass");
            user.Info.Question1 = question1;
            user.Info.Question2 = question2;
            user.Info.Answer1 = Request.Form.Get("txtAnswer1");
            user.Info.Answer2 = Request.Form.Get("txtAnswer2");
            user.Info.Name = Request.Form.Get("txtUserName");
            if (user.Insert() != 0)
            {
                string pathDirectory = Server.MapPath("~/UserData") + "/" + user.Info.ID;
                if (!Directory.Exists(pathDirectory))
                {
                    Directory.CreateDirectory(pathDirectory);
                }
                Session.Add("LoginUser", user.Info.ID);
                Response.Redirect("~/FrontEnd/Default.aspx");
            }
            else message = "Tài khoản đã tồn tại";

        }
        if (kindSubmit == "login")
        {
            String userName = Request.Form.Get("txtUserID_Login");
            String passWord = Request.Form.Get("txtPassword_Login");
            user = new BLO.User(userName,passWord);
            if (user.Info == null) message = "Đăng nhập không hợp lệ";
            else
            {
                message = "Đăng nhập thành công";
                Session.Add("LoginUser", userName);
                Response.Redirect("~/FrontEnd/Default.aspx");
            }
        };
    }
}
